#!/bin/bash

basepath=/tmp/cnilogs/
mkdir -p $basepath
secs=`date "+%s"`
confpath=${basepath}/${secs}.netconf
envpath=${basepath}/${secs}.env

env | grep CNI_CONTAINERID >/dev/null 2>&1
if [ $? -eq 0 ];then
	confpath=${basepath}/${CNI_CONTAINERID}_${CNI_IFNAME}.netconf
	envpath=${basepath}/${CNI_CONTAINERID}_${CNI_IFNAME}.env
fi

read -r line
echo $line > ${confpath}

env > ${envpath}

if [ "${CNI_COMMAND}" == "DEL" ]; then
    echo "{
    \"cniVersion\": \"0.3.1\",
    \"code\": \"0\",
    \"msg\": \"delete success\"
}"

    exit 0
fi

echo "{
    \"cniVersion\": \"0.3.1\",
    \"code\": \"1\",
    \"msg\": \"invalid configuration\"
}"
exit 1